<template>
  <div class="add-apply">
    <header class="add-header">
      <el-page-header :content="'维修申请'" @back="$router.back()" />
    </header>
    <main class="add-main">
      <div class="form-container">
        <div class="title">基本信息</div>
        <div class="form">
          <el-form ref="basicInfoForm" :model="basicInfoForm" :rules="basicInfoRules" label-width="100px">
            <el-form-item label="申请人" prop="personName">
              <el-input v-model="basicInfoForm.personName" />
            </el-form-item>
            <el-form-item label="联系方式" prop="phoneNumber">
              <el-input v-model="basicInfoForm.phoneNumber" />
            </el-form-item>
            <el-form-item label="管道编码" prop="pipeNumber">
              <el-input v-model="basicInfoForm.pipeNumber" />
            </el-form-item>
            <el-form-item label="处理地址" prop="address">
              <el-input v-model="basicInfoForm.address" />
            </el-form-item>
          </el-form>
        </div>
      </div>
      <div class="form-container">
        <div class="title">缴费信息</div>
        <!-- 缴费信息 -->
        <div class="form">
          <el-form ref="feeForm" :model="feeForm" :rules="feeFormRules" label-width="100px">
            <el-form-item label="申请日期" prop="payTime">
              <el-date-picker
                v-model="feeForm.payTime"
                type="daterange"
                range-separator="至"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
                value-format="yyyy-MM-dd"
              />
            </el-form-item>
            <el-form-item label="支付金额" prop="paymentAmount">
              <el-input v-model="feeForm.paymentAmount" />
            </el-form-item>
              </el-select>
            </el-form-item>
          </el-form>
        </div>
      </div>

    </main>
    <footer class="add-footer">
      <div class="btn-container">
        <el-button type="primary" @click="confirmAdd">确定</el-button>
      </div>
    </footer>
  </div>
</template>

<script>
import { createMaintenanceAPI } from '@/api/maintenance'
export default {
  data() {
    return {
      // 表单对象
      basicInfoForm: {
        personName: '',
        phoneNumber: '',
        pipeNumber: '',
        address: ''
      },
      // 规则对象
      basicInfoRules: {
        personName: [
          {
            required: true, message: '请输入申请人姓名', trigger: 'blur'
          }
        ],
        phoneNumber: [
          {
            required: true, message: '请输入联系方式', trigger: 'blur'
          }
        ],
        pipeNumber: [
          {
            required: true, message: '请输入管道编码', trigger: 'blur'
          }
        ],
        address: [
          {
            required: true, message: '请输入处理地址', trigger: 'blur'
          }
        ]
      },
      // 缴费信息表单
      feeForm: {
        payTime: '', // 支付时间
        paymentAmount: null // 支付金额
      },
      // 缴费规则
      feeFormRules: {
        payTime: [
          {
            required: true,
            message: '请选择申请日期'
          }
        ],
        paymentAmount: [
          {
            required: true,
            message: '请输入支付金额',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  computed: {
    // 缓存id 方便调用
    id() {
      return this.$route.query.id
    }
  },
  methods: {
    confirmAdd() {
      // 俩个表单统一校验
      // 调用实例的validate方法
      // 串行校验 第一个表单校验通过之后再开始第二个表单
      this.$refs.basicInfoForm.validate(valid => {
        if (valid) {
          // 第二个表单校验
          this.$refs.feeForm.validate(async valid => {
            if (valid) {
              // TODO API
              // 二次处理请求参数
              const reqData = {
                ...this.basicInfoForm,
                ...this.feeForm
              }
              delete reqData.payTime
              delete reqData.paymentAmount
              await createMaintenanceAPI(reqData)
              // 后续处理
              // 提示用户
              this.$message.success('维修申请成功！')
              // 跳回列表
              this.$router.back()
            }
          })
        }
      })
    }
  }
}
</script>

<style scoped lang="scss">
.add-apply {
  background-color: #f4f6f8;
  height: 100vh;

  .add-header {
    display: flex;
    align-items: center;
    padding: 0 20px;
    height: 64px;
    background-color: #fff;

    .left {
      span {
        margin-right: 4px;
      }
      .arrow{
        cursor: pointer;
      }
    }

    .right {
      text-align: right;
    }
  }

  .add-main {
    background: #f4f6f8;
    padding: 20px 130px;

    .form-container {
      background-color: #fff;

      .title {
        height: 60px;
        line-height: 60px;
        padding-left: 20px;
      }

      .form {
        margin-bottom: 20px;
        padding: 20px 65px 24px;

        .el-form {
          display: flex;
          flex-wrap: wrap;

          .el-form-item {
            width: 50%;
          }
        }
      }
    }
    .preview{
      img{
        width: 100px;
      }
    }
  }

  .add-footer {
    position: fixed;
    bottom: 0;
    width: 100%;
    padding: 24px 50px;
    color: #000000d9;
    font-size: 14px;
    background: #fff;
    text-align: center;
  }
}
</style>
